
package SOAPMisc;

import java.util.Iterator;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

import utils.variable;

/**
 * Class used to get LDAP sync status from CUCM
 */
public class getLdapSystem
	{
	/**
	 * Variables
	 */
	
	
	
	public static boolean getSyncStatus() throws Exception
		{
		String status = getSystem();
		System.out.println("LDAP Sync Enabled : "+status);
		if(status.compareTo("true")==0)
			{
			return true;
			}
		else
			{
			return false;
			}
		}
	
	/****************************************
	 * Method used to get UUID
	 ****************************************/
	private static String getSystem() throws Exception
		{
		MessageFactory mf = MessageFactory.newInstance();
		SOAPFactory soapFactory = SOAPFactory.newInstance();
	    SOAPMessage soapMessage = mf.createMessage();
	    MimeHeaders headers = soapMessage.getMimeHeaders();
	    headers.addHeader("SOAPAction", "CUCM:DB ver="+variable.getVersionCCM()+".0");
	    SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();
	    SOAPBody bdy = envelope.getBody();
	    SOAPBodyElement bodyElement = bdy.addBodyElement(soapFactory.createName("getLdapSystem","axl","http://www.cisco.com/AXL/API/"+variable.getVersionCCM()+".0"));
	    bodyElement.addAttribute(envelope.createName("sequence"), String.valueOf(System.currentTimeMillis()));
	    
		SOAPMessage soapAnswer = new SOAPGear().execute(soapMessage);
		SOAPPart replySP = soapAnswer.getSOAPPart();
	    SOAPEnvelope replySE = replySP.getEnvelope();
	    SOAPBody replySB = replySE.getBody();
	    
	    if (replySB.hasFault())
	    	{
	    	throw new Exception(replySB.getFault().getFaultString());
	    	}
	    else
	    	{
	    	return getSyncEnabled(replySB,envelope);
	    	}
		}
	
	public static String getSyncEnabled(SOAPBody replySB,SOAPEnvelope envelope) throws Exception
		{
    	Iterator iterator = replySB.getChildElements();
    	SOAPBodyElement bodyEle = (SOAPBodyElement)iterator.next();
    	
    	Iterator ite = bodyEle.getChildElements();
    	SOAPBodyElement bodyElem = (SOAPBodyElement)ite.next();
    	
    	Iterator iter = bodyElem.getChildElements();
    	SOAPBodyElement bodyEleme = (SOAPBodyElement)iter.next();
    	
    	Iterator itera = bodyEleme.getChildElements(envelope.createName("syncEnabled"));
    	SOAPBodyElement bodyElemen = (SOAPBodyElement)itera.next();
    	return bodyElemen.getValue();
		}
	
	/*2012*//*RATEL Alexandre 8)*/
	}
